package com.kedsoft.andaily.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.kedsoft.andaily.common.MessageResult;
import com.kedsoft.andaily.entity.Project;
import com.kedsoft.andaily.service.ProjectService;
import com.kedsoft.andaily.util.DateUtil;
import com.kedsoft.andaily.util.ExcelUtils;
import com.kedsoft.andaily.vo.ExcelDataVo;
import com.kedsoft.andaily.vo.PageVo;
import com.kedsoft.andaily.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-10
 */
@Controller
@RequestMapping("/project")
public class ProjectController extends BaseController {
    @Autowired
    ProjectService projectService;

    @RequestMapping("index")
    public String index(){
        return "project/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Project> getPageList(Pagination pagination, @RequestParam(required = false) String projectname,
                                        @RequestParam(required = false) String projectcode){
        EntityWrapper<Project> entityWrapper = new EntityWrapper<>();
        entityWrapper.like("projectname",projectname);
        entityWrapper.like("projectcode",projectcode);
        if(pagination.getOrder()!=null) {
            if (pagination.getOrder().toLowerCase().equals("asc")){
                entityWrapper.orderBy(pagination.getSort(), true);
            }else{
                entityWrapper.orderBy(pagination.getSort(), false);
            }
        }
        Page<Project> page = new Page<>(pagination.getPage()==null?1:pagination.getPage(),
                pagination.getRows()==null?20:pagination.getRows());

        projectService.selectPage(page,entityWrapper);
        PageVo<Project> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<Project> allList(){
        EntityWrapper<Project> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("status",1);
        return projectService.selectList(entityWrapper);
    }

    @RequestMapping(value = "form")
    public String form(){
        return "project/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Project param){
        projectService.insert(param);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Project param){
        projectService.updateById(param);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        projectService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            projectService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "project/search";
    }

    @RequestMapping(value = "excel", method = RequestMethod.POST)
    public void getExcel(HttpServletResponse response){
        EntityWrapper<Project> entityWrapper = new EntityWrapper<>();
        //entityWrapper.eq("status",1);
        List<Project> list = projectService.selectList(entityWrapper);
        ExcelDataVo excelDataVo = new ExcelDataVo();
        excelDataVo.setName("项目资料列表");
        List<String> titles = new ArrayList();
        titles.add("项目名称");
        titles.add("项目编号");
        titles.add("经办人");
        titles.add("合同金额");
        titles.add("签订时间");
        titles.add("状态");
        excelDataVo.setTitles(titles);
        List<List<Object>> rows = new ArrayList();
        for (int i=0;i<list.size();i++){
            Project bean = list.get(i);
            List<Object> row = new ArrayList();
            row.add(bean.getProjectname());
            row.add(bean.getProjectcode());
            row.add(bean.getProjectuser());
            row.add(bean.getMoney());
            row.add(DateUtil.format(bean.getStarttime(),"yyyy-MM-dd"));
            row.add(bean.getStatus()==0?"停止":"正常");
            rows.add(row);
        }
        excelDataVo.setRows(rows);
        try{
            ExcelUtils.exportExcel(response,"项目资料列表",excelDataVo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

